package cn.wenhe9.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.wenhe9.common.utils.ResultResponse;
import cn.wenhe9.common.valid.AddGroup;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import cn.wenhe9.gulimall.product.entity.BrandEntity;
import cn.wenhe9.gulimall.product.service.BrandService;

import javax.annotation.Resource;


/**
 * 品牌
 *
 * @author dujinliang
 * @email dujinliang9@163.com
 * @date 2022-04-14 19:17:08
 */
@Api(tags = "品牌管理接口")
@RestController
@RequestMapping("/api/product/brand")
public class BrandController {
    @Resource
    private BrandService brandService;

    /**
     * 分页获取品牌列表
     * @param page Long
     * @param limit Long
     * @param key String
     * @return ResultResponse
     */
    @ApiOperation("分页获取品牌列表")
    @GetMapping("/list")
    public ResultResponse list(@RequestParam(defaultValue = "0") Long page, @RequestParam(defaultValue = "10") Long limit, String key){
        Page<BrandEntity> pmsBrandPage = brandService.getBrandByPage(page, limit, key);
        return ResultResponse.ok(pmsBrandPage);
    }

    @ApiOperation("保存新的品牌")
    @PostMapping("/save")
    public ResultResponse saveBrand(@Validated({AddGroup.class}) @RequestBody BrandEntity brand){
        brandService.save(brand);
        return ResultResponse.ok();
    }

    @ApiOperation("更新品牌信息")
    @PostMapping("/update")
    public ResultResponse updateBrand(@RequestBody BrandEntity brand){
        brandService.updateDetail(brand);
        return ResultResponse.ok();
    }

    @ApiOperation("更新品牌状态信息")
    @PostMapping("/update/status")
    public ResultResponse updateBrandStatus(@RequestBody BrandEntity brand){
        brandService.update().eq("brand_id", brand.getBrandId()).set("show_status", brand.getShowStatus());
        return ResultResponse.ok();
    }

    @ApiOperation("根据id查询品牌信息")
    @GetMapping("/info/{brandId}")
    public ResultResponse getBrandById(@PathVariable(name = "brandId") Long brandId){
        BrandEntity pmsBrand = brandService.getById(brandId);
        return ResultResponse.ok(pmsBrand);
    }

    @ApiOperation("批量删除品牌信息")
    @DeleteMapping("/delete")
    public ResultResponse batchDeleteByIds(@RequestBody List<Long> ids){
        brandService.batchDeleteByIds(ids);
        return ResultResponse.ok();
    }
}
